const HtmlWebpackPlugin = require('html-webpack-plugin');
const prettier = require('prettier');
const config = require('../prettier.config');

/**
 * 代码格式化
 */
class Format {
  apply(compiler) {
    compiler.hooks.compilation.tap('Format', (compilation) => {
      HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync('Format', async (data, cb) => {
        // prettier格式化
        data.html = await prettier.format(data.html, { ...config, parser: 'html' });
        cb(null, data);
      });
    });
  }
}

module.exports = Format;
